<template>
    <jb-crud-page
        ref="bucketPage"
        title-icon="mdi:account-outline"
        title-text="七牛Bucket管理"
        search-url="/api/admin/qiniuBucket/datas"
        :search-conditions="pageConditions"
        :edit-component="BucketEdit"
    >
        <template #conditions-form>
            <n-input
                v-model:value="pageConditions.keywords"
                type="text"
                placeholder="输入关键字搜索"
            />

            <jb-select
                v-model:value="pageConditions.qiniuId"
                url="/api/admin/qiniu/options"
                placeholder="=所属账号="
                filterable
                class="w-130px"
            >
            </jb-select>
            <jb-select
                v-model:value="pageConditions.zone"
                url="/api/admin/dictionary/options?typeKey=qiniu_region"
                filterable
                class="w-130px"
                placeholder="=地区="
            >
            </jb-select>
        </template>
        <template #opt>
            <jb-btn
                :icon="Icons.REFRESH"
                ghost
                type="primary"
                url="/api/admin/qiniuBucket/syncAllQiniuBuckets"
                @success="bucketPage?.loadData()"
            >
                同步所有Bucket
            </jb-btn>
        </template>
        <template #default="{ state }">
            <vxe-table
                border
                :column-config="{ resizable: true }"
                height="auto"
                show-header-overflow
                show-overflow="tooltip"
                :seq-config="{ startIndex: state.tableStartIndex }"
                :row-config="{ isHover: true }"
                align="center"
                :data="state.list"
            >
                <jb-column type="seq" title="序号" width="60" fixed="left"></jb-column>
                <jb-column
                    field="name"
                    title="bucket名称"
                    min-width="200"
                    fixed="left"
                ></jb-column>
                <jb-column
                    field="sn"
                    title="编码(SN)"
                    min-width="200"
                    fixed="left"
                ></jb-column>
                <jb-column
                    field="qiniuName"
                    title="所属七牛账号"
                    min-width="200"
                ></jb-column>
                <jb-column
                    field="regionName"
                    title="所属地区"
                    min-width="160"
                ></jb-column>
                <jb-column field="domainUrl" title="绑定域名" min-width="300"></jb-column>
                <jb-column
                    field="callbackUrl"
                    title="回调地址"
                    min-width="300"
                ></jb-column>
                <jb-column
                    field="callbackBody"
                    title="回调body定义"
                    min-width="300"
                ></jb-column>
                <jb-column
                    field="callbackBodyType"
                    title="回调body类型"
                    min-width="300"
                ></jb-column>
                <jb-column field="expires" title="有效期(妙)" min-width="160"></jb-column>

                <jb-column field="isDefault" title="是否默认" width="80" fixed="right">
                    <template #default="{ row }">
                        <jb-switch
                            v-model:value="row.isDefault"
                            field-name="isDefault"
                            :url="`/api/admin/qiniuBucket/toggleIsDefault/${row.id}`"
                            @change="bucketPage?.loadData()"
                        ></jb-switch>
                    </template>
                </jb-column>
                <jb-column field="enable" title="是否启用" width="80" fixed="right">
                    <template #default="{ row }">
                        <jb-switch
                            v-model:value="row.enable"
                            :url="`/api/admin/qiniuBucket/toggleEnable/${row.id}`"
                        ></jb-switch>
                    </template>
                </jb-column>
                <jb-column field="remark" title="备注" width="80">
                    <template #default="{ row }">
                        <jb-btn
                            tip-text="查看"
                            :icon="Icons.EYE"
                            quaternary
                            circle
                            strong
                            modal-title="查看备注"
                            :modal-component="AccountRemark"
                            :modal-args="{ negativeText: '关闭' }"
                            :modal-component-props="{ remark: row.remark }"
                        ></jb-btn>
                    </template>
                </jb-column>
                <jb-column
                    field="createTime"
                    title="创建时间"
                    min-width="200"
                ></jb-column>
                <jb-column
                    field="updateTime"
                    title="更新时间"
                    min-width="200"
                ></jb-column>
                <jb-column
                    field="createUserName"
                    title="创建人"
                    min-width="160"
                ></jb-column>
                <jb-column
                    field="updateUserName"
                    title="更新人"
                    min-width="160"
                ></jb-column>
                <jb-column title="操作" width="110" fixed="right">
                    <template #default="{ row }">
                        <jb-btn
                            tip-text="编辑"
                            :icon="Icons.EDIT"
                            quaternary
                            circle
                            secondary
                            type="warning"
                            strong
                            @click="
                                bucketPage?.showEditModal('修改七牛Bucket', {
                                    id: row.id
                                })
                            "
                        ></jb-btn>
                        <jb-btn
                            tip-text="删除"
                            :icon="Icons.DELETE"
                            type="error"
                            secondary
                            quaternary
                            circle
                            strong
                            class="mx-8px"
                            confirm-text="确定删除这条数据？"
                            :url="`/api/admin/bucket/delete/${row.id}`"
                            @success="bucketPage?.loadData()"
                        ></jb-btn>
                    </template>
                </jb-column>
            </vxe-table>
        </template>
    </jb-crud-page>
</template>

<script setup lang="ts">
import { onBeforeMount, ref } from 'vue'
import { useRoute } from 'vue-router'
import { Icons } from '@/constants'
import JbCrudPage from '@/components/_builtin/jb-crud-page/index.vue'
import AccountRemark from '@/views/_builtin/management/qiniu/account/components/account-remark/index.vue'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import BucketEdit from './components/bucket-edit/index.vue'

const route = useRoute()

const bucketPage = ref<InstanceType<typeof JbCrudPage> | null>(null)
const pageConditions = useResetableData({
    keywords: '',
    qiniuId: '',
    zone: ''
})
onBeforeMount(async () => {
    pageConditions.qiniuId = route.query?.qiniuId as string
})
</script>

<style scoped></style>
